package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.HeaderUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.form.PageResponse;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author LY
 * @since 2022/06/17 21:10
 */
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;


    // 创建订单
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        // 从请求头获取userId
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(jsonObject.get("uid").toString());

        request.setUserId(userId);
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);

        if(OrderRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil().setData(response.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 获取当前用户的所有订单
    @GetMapping("order")
    public ResponseData getOrder(Integer page, Integer size, String sort, HttpServletRequest servletRequest){
        Long userId = HeaderUtil.getUserId(servletRequest);

        OrderListRequest request = new OrderListRequest();
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);
        request.setUserId(userId);
        OrderListResponse response = orderQueryService.orderList(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(response.getDetailInfoList());
            pageResponse.setTotal(response.getTotal());
            return new ResponseUtil().setData(pageResponse);
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());

    }

    // 获取订单详情
    @GetMapping("order/{id}")
    public ResponseData getOrderDetail(@PathVariable("id") String orderId){
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(orderId);
        OrderDetailResponse response = orderQueryService.orderDetail(request);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            OrderDetail orderDetail = OrderDetailResponse2OrderDetail(response);
            return new ResponseUtil().setData(orderDetail);
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 取消订单
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest request){
        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response.getMsg());
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 删除订单
    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId){
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(orderId);
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response.getMsg());
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());

    }

    private OrderDetail OrderDetailResponse2OrderDetail(OrderDetailResponse response) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setUserName(response.getOrderShippingDto().getReceiverName());
        orderDetail.setOrderTotal(response.getPayment());
        orderDetail.setUserId(response.getUserId());
        orderDetail.setGoodsList(response.getOrderItemDto());
        orderDetail.setTel(response.getOrderShippingDto().getReceiverMobile());
        orderDetail.setStreetName(response.getOrderShippingDto().getReceiverAddress());
        orderDetail.setOrderStatus(response.getStatus());
        return orderDetail;
    }
}
